package com.ruoyi.wechat.domain;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;

import java.io.Serializable;
import java.util.Date;

/**
 * @Author: yan
 * @Description: 微信消息
 * @Date: create in 2021/11/8 10:12
 */
@Data
@TableName("wx_wx_msg")
public class WxWxMsg implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @ApiModelProperty(name = "id", value = "主键ID")
    @TableId
    private Long id;
    @ApiModelProperty(name = "appid", value = "appid")
    private String appid;
    /**
     * 微信用户ID
     */
    @ApiModelProperty(name = "openid", value = "公众号用户ID")
    private String openid;
    /**
     * 消息方向
     */
    @ApiModelProperty(name = "inOut", value = "消息方向")
    private String inOut;
    /**
     * 消息类型
     */
    @ApiModelProperty(name = "msgType", value = "消息类型")
    private String msgType;
    /**
     * 消息详情
     */
    @ApiModelProperty(name = "detail", value = "消息详情")
    private String detail;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(name = "createTime", value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "头像")
    private String headimgurl;
    @ApiModelProperty(value = "手机号")
    private String phone;
    @ApiModelProperty(value = "关注场景")
    private String subscribeScene;
    @ApiModelProperty(value = "关注场景值")
    private String qrSceneStr;

    public static class WxMsgInOut {
        static final String IN = "0";
        static final String OUT = "1";
    }

    public WxWxMsg() {
    }

    public WxWxMsg(WxMpXmlMessage wxMessage) {
        JSONObject jsonObject = new JSONObject();
        this.openid = wxMessage.getFromUser();
        this.appid = WxMpConfigStorageHolder.get();
        this.inOut = WxMsgInOut.IN;
        this.msgType = wxMessage.getMsgType();
        this.detail = wxMessage.toString();
        Long createTime = wxMessage.getCreateTime();
        this.createTime = createTime == null ? new Date() : new Date(createTime * 1000);
        if (WxConsts.XmlMsgType.TEXT.equals(this.msgType)) {
            jsonObject.put("content", wxMessage.getContent());
        } else if (WxConsts.XmlMsgType.IMAGE.equals(this.msgType)) {
            jsonObject.put("picUrl", wxMessage.getPicUrl());
            jsonObject.put("mediaId", wxMessage.getMediaId());
        } else if (WxConsts.XmlMsgType.VOICE.equals(this.msgType)) {
            jsonObject.put("format", wxMessage.getFormat());
            jsonObject.put("mediaId", wxMessage.getMediaId());
        } else if (WxConsts.XmlMsgType.VIDEO.equals(this.msgType) ||
                WxConsts.XmlMsgType.SHORTVIDEO.equals(this.msgType)) {
            jsonObject.put("thumbMediaId", wxMessage.getThumbMediaId());
            jsonObject.put("mediaId", wxMessage.getMediaId());
        } else if (WxConsts.XmlMsgType.LOCATION.equals(this.msgType)) {
            jsonObject.put("locationX", wxMessage.getLocationX());
            jsonObject.put("locationY", wxMessage.getLocationY());
            jsonObject.put("scale", wxMessage.getScale());
            jsonObject.put("label", wxMessage.getLabel());
        } else if (WxConsts.XmlMsgType.LINK.equals(this.msgType)) {
            jsonObject.put("title", wxMessage.getTitle());
            jsonObject.put("description", wxMessage.getDescription());
            jsonObject.put("url", wxMessage.getUrl());
        } else if (WxConsts.XmlMsgType.EVENT.equals(this.msgType)) {
            jsonObject.put("event", wxMessage.getEvent());
            jsonObject.put("eventKey", wxMessage.getEventKey());
        }
    }

    public static WxWxMsg buildOutMsg(String msgType, String openid, JSONObject detail) {
        WxWxMsg wxMsg = new WxWxMsg();
        wxMsg.appid = WxMpConfigStorageHolder.get();
        wxMsg.msgType = msgType;
        wxMsg.openid = openid;
        wxMsg.detail = detail.toString();
        wxMsg.createTime = new Date();
        wxMsg.inOut = WxMsgInOut.OUT;
        return wxMsg;
    }
}
